<?php

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Service_MxShopPage extends WebPage {

    public function main()
    {
        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        $condition = " deleted=0";
        $params = array();

        $status = intval($_GPC['status']);
        if (!empty($status))
        {

            if($status == 1)
            {
                $status2 = 1;
            }
            else
            {
                $status2 = 0;
            }
            $condition .= " AND status=:status";
            $params[':status'] = $status2;
        }

        if (!(empty($_GPC['keyword'])))
        {
            $condition .= ' AND (locate(:keyword,nickname)>0 OR locate(:keyword,account)>0)';
            $params[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
        }

        $list = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_role_channel') . " WHERE " . $condition . "  ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ",{$psize}", $params);
        foreach ($list as $key => $item){
            $list[$key]['mendian'] = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_mendian') . " WHERE deleted=0 AND channelid=:channelid ", array(':channelid'=>$item['id']) );
            $comm = pdo_fetch("SELECT id,total FROM " . tablename('mx_shop_service_commission') . " WHERE uid=:uid", array(':uid'=>$item['id']) );
            $list[$key]['comid'] = $comm['id'];
            $list[$key]['comm'] = $comm['total'] > 0 ? $comm['total'] : 0;
        }

        $total = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_role_channel') . " WHERE " . $condition, $params);
        $pager = pagination($total, $pindex, $psize);

        include $this->template();
    }

    public function post()
    {
        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $id = intval($_GPC['id']);

        if (!empty($id)) {
            $item = pdo_fetch("SELECT * FROM " . tablename('mx_shop_role_channel') . " WHERE id=:id AND deleted=0", array(':id' => $id));

        } else {
            $item = array(
                'status' => 1,
            );
        }

        if ($_W['ispost']) {

            if (empty($_GPC['nickname'])) {
                show_json(0,"请输入服务商姓名");
            }

            if (empty($_GPC['account'])) {
                show_json(0,"请输入账号");
            }

            $condition = ' id != :id AND deleted=0 AND (nickname=:nickname OR account=:account)';
            $params = array(
                ':id' => $id,
                ':nickname' => $_GPC['nickname'],
                ':account' => $_GPC['account']
            );

            $opid = pdo_fetch("SELECT id FROM " . tablename('mx_shop_role_channel') . " WHERE ".$condition, $params);
            if(!(empty($opid['id']))){
                show_json(0,"该服务商或账号已存在");
            }

            $data = array(
                'uniacid' => $uniacid,
                'nickname' => trim($_GPC['nickname']),
                'thumb' => $_GPC['thumb'],
                'account' => trim($_GPC['account']),
                'status' => intval($_GPC['status']),
                'updatetime' => TIMESTAMP,
            );

            if (empty($id))
            {
                if(empty($_GPC['password']) || strlen($_GPC['password']) < 6 || strlen($_GPC['password']) >20)
                {
                    show_json(0,"请输入6~20位的密码");
                }

                $salt = random(32);
                $data['password'] = md5(md5($_GPC['password']).$salt);
                $data['salt'] = $salt;
                $data['createtime'] = TIMESTAMP;
                pdo_insert('mx_shop_role_channel', $data);
                $id = pdo_insertid();
                plog('mendian.service.post', '新建服务商信息成功！服务商ID为' . $id);
            }
            else
            {

                if(!empty($_GPC['password']) )
                {
                    if(strlen($_GPC['password']) < 6 || strlen($_GPC['password']) >20)
                    {
                        show_json(0, "请输入6~20位的密码");
                    }

                    $salt = random(32);
                    $data['password'] = md5(md5($_GPC['password']).$salt);
                    $data['salt'] = $salt;
                }

                pdo_update('mx_shop_role_channel', $data, array('id' => $id));
                plog('mendian.service.post', '更新服务商信息成功！服务商ID为' . $id);
            }

            show_json(1,array('url'=>webUrl('manage/service')));

        }

        include $this->template();

    }

    public function status(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        $status = intval($_GPC['status']);

        if(!empty($id)){
            if($status == 1){
                pdo_update('mx_shop_role_channel', array( 'status' => 1), array('id' => $id));
            }else{
                pdo_update('mx_shop_role_channel', array( 'status' => 0), array('id' => $id));
            }
        }
        show_json(1, array('url'=>webUrl('manage/service')));

    }

    public function delete(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        if (empty($id)) {
            $id = is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0;
        }

        $cates = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_role_channel') . " WHERE id in( $id ) ");
        foreach ($cates as $cate) {
            pdo_update('mx_shop_role_channel', array('deleted' => 1, 'status' => 0), array('id' => $cate['id']));
            plog('manage.service.delete', "删除服务商 <br/>服务商信息:  ID: {$cate['id']}");
        }
        show_json(1, array('url' => referer()));

    }

}
